const isArray = (arr: any) => {
    return Object.prototype.toString.call(arr) === '[object Array]';
}
//深拷贝
export const deepClone = (obj: any) => {
    // 对常见的“非”值，直接返回原来值
    if ([null, undefined, NaN, false].includes(obj)) return obj;
    if (typeof obj !== "object" && typeof obj !== 'function') {
        //原始类型直接返回
        return obj;
    }
    const o: any = isArray(obj) ? [] : {};
    for (const i in obj) {
        if (obj.hasOwnProperty(i)) {
            o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
        }
    }
    return o;
}
export function localFilterData(allData: any[], searchText: string, matchColumn = ['key', 'label']) {
    // 如果没有搜索内容，直接返回所有数据
    if (searchText === '') return allData;
    // 关键字拆分成数组
    const searchTextArr = searchText.split(' ');
    // 数据项和搜索关键字进行匹配，有符合条件的返回该item，并根据匹配次数，增加rank属性
    const matchResult = (item: Record<string, any>) => {
        let isConform = false;
        let rank = 0;
        searchTextArr.forEach((keyWord) => {
            const reg = new RegExp(keyWord);
            matchColumn.forEach((column) => {
                if (reg.test(item[column])) {
                    rank++;
                    isConform = true;
                }
            });
        });
        return isConform ? { ...item, rank } : undefined;
    };
    let result: any[] = [];
    // 递归便遍历所有数据
    const recursiveData = (arr: any[]) => {
        arr.forEach((item) => {
            const matchItem = matchResult(item);
            if (matchItem) result.push(matchItem);
            if (item.children instanceof Array && item.children.length > 0) {
                result = recursiveData(item.children);
            }
        });
        return result;
    };
    return recursiveData(allData).sort((a, b) => {
        return b.rank - a.rank;
    });
}
